package ro.ubbcluj.cs.bibliospringee.api.resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.ubbcluj.cs.bibliospringee.api.assembler.Assembler;
import ro.ubbcluj.cs.bibliospringee.api.assembler.AuthorAssembler;
import ro.ubbcluj.cs.bibliospringee.api.exception.BiblioSpringApiException;
import ro.ubbcluj.cs.bibliospringee.backend.model.Author;
import ro.ubbcluj.cs.bibliospringee.backend.service.AuthorService;
import ro.ubbcluj.cs.bibliospringee.backend.service.exception.BiblioSpringServiceException;
import ro.ubbcluj.cs.bibliospringee.common.dto.AuthorDto;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@LocalBean
@Stateless
@Path("/author")
public class AuthorResource
{
    private final Logger logger = LoggerFactory.getLogger(AuthorResource.class);

    private final Assembler<Author, AuthorDto> authorAssembler = new AuthorAssembler();
    @EJB
    private AuthorService authorService;

    @GET
    public Response getAllAuthor()
    {
        logger.debug("@GET - get all authors");
        final List<Author> authors = authorService.getAll();
        final List<AuthorDto> authorDtos = new ArrayList<>();

        for (final Author author : authors)
        {
            authorDtos.add(authorAssembler.modelToDto(author));
        }
        final GenericEntity<List<AuthorDto>> genericAuthorDtos = new GenericEntity<List<AuthorDto>>(authorDtos)
        {
        };

        return Response.ok(genericAuthorDtos).build();
    }

    @GET
    @Path("{id}")
    public Response getAuthorById(@PathParam("id") final Long id)
    {
        try
        {
            final Author author = authorService.getById(id);
            return Response.ok(authorAssembler.modelToDto(author)).build();
        }
        catch (final BiblioSpringServiceException bbse)
        {
            logger.error("@GET by id error!", bbse);
            throw new BiblioSpringApiException("@GET by id error!", bbse);
        }
    }

    @POST
    public Response insertAuthor(AuthorDto authorDto)
    {
        try
        {
            Author author = authorAssembler.dtoToModel(authorDto);
            author = authorService.insertAuthor(author);
            return Response.ok(authorAssembler.modelToDto(author)).build();
        }
        catch (final BiblioSpringServiceException bbse)
        {
            logger.error("@GET by id error!", bbse);
            throw new BiblioSpringApiException("@GET by id error!", bbse);
        }
    }

    @Path("/{authorId}")
    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public Response deleteAuthor(@PathParam("authorId") final long authorId)
    {
        try
        {
            final Author author = authorService.getById(authorId);
            authorService.deleteAuthor(author);

            return Response.ok().build();
        }
        catch (BiblioSpringServiceException bbse)
        {
            throw new BiblioSpringApiException("@DELETE by id error!", bbse);
        }
    }
}